package com.sxdz.constant

import groovy.transform.CompileStatic
import groovy.transform.TypeChecked

/**
 * Created with IntelliJ IDEA.
 * User: 张斌
 */


@CompileStatic
@TypeChecked
class CommonConstant {

    final static Map<String,Map> CacheConstant = [:]

    final static String ConstantName = "com.sxdz.constant.CommonConstant\$"

    /**
     * 根据 enum类型和 值 获取描述
     * @param enumClass
     * @param value
     * @return
     */
    static String ConstantValueOfName(Class enumClass , String value){
        String res
        enumClass.getEnumConstants().find {Class it->
            if (it.getAt("value") == value){
                res = it.getAt("name")
                return true
            }
        }
        res
    }

    /**
     * 根据 enum名称 获取描述
     * @param enumName
     * @param value
     * @return
     */
    static String ConstantValueOfName(String enumName , String value){
        Map conMap = CacheConstant[enumName]
        if (conMap==null){
            conMap = [:]
            Class enumClass = Class.forName(ConstantName+enumName)
            enumClass.getEnumConstants().each {Enum it->
                conMap << [(it.getAt("value")):it.getAt("name")]
            }
            CacheConstant << [(enumName):conMap]
        }
        conMap[value]
    }

    /**
     * 获取 enum 类型的值和描述，返回是map
     * @param enumClass
     * @return
     */
    static Map getConstantMap(String enumName){
        Map res = CacheConstant[enumName]
        if (res==null){
            res = [:]
            Class enumClass = Class.forName(ConstantName+enumName)
            enumClass.getEnumConstants().each {Enum it->
                res << [(it.getAt("value")):it.getAt("name")]
            }
            CacheConstant << [(enumName):res]
        }
        res
    }

    /**
     * 返回成功 编码
     */
    static final String RE_SUCCESS_CODE = "0000"
    /**
     * 返回失败 编码
     */
    static final String RE_ERROR_CODE="1000"
    /**
     * 错误信息
     */
    static final String code="2000"
    /**
     * 错误代码
     */
    enum ErrorMsg{
        E1001("openId失效，需要重新发起操作","1001"),
        E1002("code 失效","1002"),
        E1005("渠道交易已关闭","1005"),
        E2001("绑定失败","2001"),
        E2002("解除绑定失败","2002"),
        E2003("绑定账号不存在","2003"),
        E2005("非服务器时间","2005"),
        E9001("参数错误","9001"),
        E404("访问的页面不存在","404"),
        E405("没有查询到需要的数据","405"),
        E9991("户号错误","9991")

        String name,value

        ErrorMsg(String name,String value){
            this.name = name
            this.value = value
        }
    }

    /**
     * 有效
     */
    enum IsActive{
        True("有效","0"),
        False("无效","1")

        String name,value

        IsActive(String name,String value){
            this.name = name
            this.value = value
        }
    }

    /**
     * 是否有缴费成功纪录
     */
    enum HasSuccess{
        True("有","1"),
        False("无","0")

        String name,value

        HasSuccess(String name,String value){
            this.name = name
            this.value = value
        }
    }


    /**
     * 关注状态
     */
    enum AttentionStatus{
        Attention ("关注","0"),
        UnAttention("取消关注","1")

        String name,value

        AttentionStatus(String name,String value){
            this.name = name
            this.value = value
        }
    }

    /**
     * 支付状态
     */
    enum PayStatus{
        NotPay ("未支付","00"),
        PaySuccess("支付成功","10"),
        PayFail("支付失败","20"),
        RefundSuccess("退款成功","30"),
        RefundFail("退款失败","31")
        String name,value

        PayStatus(String name,String value){
            this.name = name
            this.value = value
        }
        static String getNameByValues(String value){
          if(NotPay.value.equals(value)){
              return NotPay.name
          }else if(PaySuccess.value.equals(value)){
              return PaySuccess.name
          }else if(PayFail.value.equals(value)){
              return PayFail.name
          }else if(RefundSuccess.value.equals(value)){
              return RefundSuccess.name
          }else if(RefundFail.value.equals(value)){
              return RefundFail.name
          }


        }
    }

    /**
     * 支付状态
     */
    enum PayResult{
        ResultSuccess ("缴费成功","0"),
        ResultFail("缴费失败","1")
        String name,value

        PayResult(String name,String value){
            this.name = name
            this.value = value
        }
    }

    /**
     * 订单状态
     */
    enum OrderStatus{
        WSH ("未审核","0"),
        YQR("已确认","1"),
        BHZ("备货中","2"),
        YFH("已发货","3"),
        YWC("已完成","4"),
        YGB("已关闭","5"),
        YQX("已取消","6")
        String name,value

        OrderStatus(String name,String value){
            this.name = name
            this.value = value
        }
    }

}
